#coding:utf-8
from flask import Flask,request
import globalvar
import demjson
import cmodel
import os

globalvar._init() #全局变量存储器初始化
app = Flask(__name__)
json = {'Content-Type': 'application/json'}

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/addserver',methods=['POST'])
def addserver():
    res = cmodel.addServer(request.form['IP'], request.form['PASSWORD'])
    return demjson.encode({'status':res}),json

@app.route('/servers')
def servers():
    servers = cmodel.getServers()
    return demjson.encode(servers),json

@app.route('/delserver/<ip>')
def delserver(ip):
    res = cmodel.delServer(ip)
    return demjson.encode({'status':res}),json

@app.route('/tasks')
def tasks():
    tasklist = cmodel.getTaskList()
    return demjson.encode(tasklist),json

@app.route('/addTaskLogs',methods=['POST'])
def addTaskLogs():
    log = request.form['LOG']
    ip = request.remote_addr
    TaskID = request.form['TASKID']
    cmodel.addTaskLogs(TaskID,ip,log)
    return demjson.encode({'status':True}),json

@app.route('/TaskLogs/<taskid>')
def TaskLogs(taskid):
    return demjson.encode(cmodel.getTaskLogs(taskid)), json

@app.route('/addTask',methods=['POST'])
def addTask():
    res = cmodel.addTask(request.form['name'],request.form['taskinfo'])
    return demjson.encode({'status':res}),json

@app.route('/addResult',methods=['POST'])
def addResult():
    res = cmodel.addResult(request.form['TaskID'],request.form['Result'])
    return demjson.encode({'status': res}), json
@app.route('/scanport',methods=['POST'])
def scanport():
    port = request.form['port']
    Band = request.form['Band']
    cmodel.batchZmap(port,Band)
    return demjson.encode({'status': 'success'}), json
@app.route('/batchShell',methods=['POST'])
def batchShell():
    shellcode = request.form['shellcode']
    if os.path.exists(shellcode) == True:
        shellcode = open(shellcode).read()

    res = cmodel.batchShell(shellcode)
    return demjson.encode({'status': res}), json
@app.route('/zgrab',methods=['POST'])
def zgrab():
    config = request.form['config']
    cmodel.Zgrab(config)
    return demjson.encode({'status': 'success'}), json
if __name__ == '__main__':
    app.run(host='0.0.0.0')